package com.topvision.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

public class ArApiUtil {


    /**
     * 鉴权
     *
     * @return
     */
    public static String getAccessToken() {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("app_key", Const.FACE_APP_KEY);
        map.put("app_secret", Const.FACE_APP_SECRET);
        map.put("grant_type", "authorization_code");
        String json = HttpClientUtil.getHttpsGetResult(Const.FACE_APP_URL + "/oauth2/access_token", map);
        JSONObject jsonObject = JSON.parseObject(json);
        return jsonObject.getString("access_token");
    }


    /**
     * 组相关API
     *
     * @param subUrl
     * @param paramMap
     * @return
     */
    public static JSONObject groupApi(String subUrl, Map<String, Object> paramMap) {

        Map<String, Object> headermap = new LinkedHashMap<>();
        headermap.put("app_key", Const.FACE_APP_KEY);
        headermap.put("access_token", getAccessToken());
        headermap.put("Content-Type", "application/x-www-form-urlencoded");
        String json = HttpClientUtil.getHttpsPostResult(Const.FACE_APP_URL + subUrl, paramMap, headermap);
        return JSON.parseObject(json);
    }


    /**
     * 创建组
     *
     * @param groupCode
     * @param name
     * @return
     */
    public static JSONObject createGroup(String groupCode, String name) {
        Map<String, Object> parammap = new LinkedHashMap<>();

        parammap.put("groupId", groupCode);
        parammap.put("tag", name);


        return groupApi("/face/group/create", parammap);
    }

    /**
     * 删除组
     */
    public static JSONObject delGroup(String groupCode) {
        Map<String, Object> parammap = new LinkedHashMap<>();

        parammap.put("groupId", groupCode);

        return groupApi("/face/group/delete", parammap);
    }

    /**
     * 人脸图片相关API
     *
     * @param subUrl
     * @param paramMap
     * @return
     */
    public static JSONObject faceMultiApi(String subUrl, Map<String, Object> paramMap) {

        Map<String, Object> headermap = new LinkedHashMap<>();
        headermap.put("app_key", Const.FACE_APP_KEY);
        headermap.put("access_token", getAccessToken());


        String json = HttpClientUtil.postMultipartResult(Const.FACE_APP_URL + subUrl, paramMap, headermap);
        return JSON.parseObject(json);
    }


    /**
     * 添加人脸图片
     */
    public static JSONObject addFace(Map<String, Object> parammap) {

        return faceMultiApi("/face/group/add", parammap);
    }

    /**
     * 删除人脸图片
     */
    public static JSONObject delFace(Map<String, Object> parammap) {

        return groupApi("/face/group/remove", parammap);
    }


    /**
     * 添加人脸图片
     */
    public static JSONObject addImg(Map<String, Object> parammap) {

        return faceMultiApi("/mediaGallery/import", parammap);
    }


    /**
     * 添加人脸图片
     */
    public static JSONObject recImg(Map<String, Object> parammap) {

        return faceMultiApi("/siftRecog", parammap);
    }



    public static void main(String[] args) {
        File file = new File("F:\\temp\\liudehua.jpg");
        Map<String, Object> parammap = new HashMap<>();
        // parammap.put("filetag", "liudehua");
        parammap.put("file", file);
        JSONObject jsonObject = recSmartFace(parammap);
        String code = UUID.randomUUID().toString();
        System.out.println(code);
    }

    /**
     * 讯思雅人脸识别--添加人脸
     *
     * @return
     */
    public static JSONObject addSmartFace(Map<String, Object> parammap) {
        Map<String, Object> map = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dbname", "face");
        jsonObject.put("filetag", parammap.get("filetag"));
        String jsonstr = jsonObject.toJSONString();
        map.put("SmartVisionRequest", jsonstr);
        map.put("file", parammap.get("file"));
        String json = HttpClientUtil.postMultipartResult(Const.API_Face_REC_URL + "/smartvisionapi/face/add", map, null);
        if (json != null) {
            JSONObject jo = JSON.parseObject(json);
            JSONArray array = JSONArray.parseArray(jo.get("result").toString());
            JSONObject jsonObj = array.getJSONObject(0);
            return jsonObj;
        }
        return null;

    }


    /**
     * 讯思雅人脸识别--识别人脸
     *
     * @return
     */
    public static JSONObject recSmartFace(Map<String, Object> parammap) {
        Map<String, Object> map = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dbname", "face");
        String jsonstr = jsonObject.toJSONString();
        map.put("SmartVisionRequest", jsonstr);
        map.put("file", parammap.get("file"));
        String json = HttpClientUtil.postMultipartResult(Const.API_Face_REC_URL + "/smartvisionapi/face/query", map, null);
        if (json != null) {
            JSONObject jo = JSON.parseObject(json);
            JSONArray array = JSONArray.parseArray(jo.get("results").toString());
            JSONObject jsonObj = array.getJSONObject(0);
            JSONObject faceResultsJson0 = (JSONObject) JSONArray.parseArray(jsonObj.get("faceResults").toString()).get(0);
            JSONObject detects0 = (JSONObject) JSONArray.parseArray(faceResultsJson0.get("detects").toString()).get(0);
            return detects0;
        }
        return null;

    }

}
